import java.util.ArrayList;
import java.util.Scanner;

public class ShoppingCar extends Product {
        public void addToCar(ArrayList<Product>goodsList,ArrayList<Product>shoppingCar) {
            Scanner reader = new Scanner(System.in);
            System.out.println("请输入您要添加的商品编号：");
            String goodscode = reader.next();
            System.out.println("请输入您要添加的商品数量：");
            int num = 0;
            try {
                num = reader.nextInt();
            } catch (Exception e) {
                System.out.println("请输入有效的数量！");
                reader.next(); // 清除错误输入
                return;
            }
            boolean added = false; // 标记是否成功添加商品
            boolean found = false; // 标记是否找到商品
            for (Product goods : goodsList) {
                if (goodscode.equals(goods.getCode())) {
                    found = true; // 找到商品
                    if (goods.getNum() >= num) {
                        shoppingCar.add(goods); // 添加商品到购物车
                        System.out.println("加购成功！");
                        added = true; // 设置标记为成功
                    } else {
                        System.out.println("库存不足，无法添加该商品。");
                    }
                    break; // 找到商品后退出循环
                }
            }
            for(Product good:shoppingCar){
                if(goodscode.equals(good.getCode())){
                    good.setNum(num);
                }
            }
        }
            public void remove (ArrayList < Product > shoppingCar) {
                System.out.println("请输入您想移除的商品名称：");
                Scanner reader = new Scanner(System.in);
                String name = reader.next();
                for (Product goods : shoppingCar) {
                    if (name.equals(goods.getGoodsName())) {
                        shoppingCar.remove(name);
                    } else {
                        System.out.println("购物车中没有您想要删除的商品！");
                    }
                }
            }
            public void changeNum (ArrayList < Product > shoppingCar) {
                Scanner reader = new Scanner(System.in);
                System.out.println("请输入您想修改数量的商品编号：");
                String goodscode = reader.next();
                for (Product product : shoppingCar) {
                    System.out.println(product);
                    if (goodscode.equals(product.getCode())) {
                        System.out.println("请输入您想要的数量：");
                        int num = reader.nextInt();
                        product.setNum(num);
                        System.out.println();
                    }
                }
            }
            public void showCar(ArrayList<Product>shoppingCar){
                for(int i=0;i<shoppingCar.size();i++){
                    if(shoppingCar.isEmpty()){
                        System.out.println("该购物车是空的！");
                    }
                    else{System.out.println(shoppingCar.get(i));}
                }
            }
        }
